﻿using Liang.Device.Camera.Dll;
using Liang.Device.Model.Camera;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Liang.Device.Camera.Action
{
    public sealed class DaHuaVMSCamera : ICamera<DaHuaVMS>
    {
        /// <summary>
        /// 静态只读实例，创建对象实例的唯一地方，加上只读属性可保证线程安全
        /// </summary>
        private static readonly DaHuaVMSCamera _instance = new DaHuaVMSCamera();

        /// <summary>
        /// 获取当前对象实例的唯一入口
        /// </summary>
        public static DaHuaVMSCamera Instance
        {
            get { return _instance; }
        }

        /// <summary>
        /// 判断摄像头是否已初始化SDK
        /// </summary>
        /// 
        private static bool isSDKInited = false;

        public static bool IsSDKInited
        {
            get { return DaHuaVMSCamera.isSDKInited; }
            set { DaHuaVMSCamera.isSDKInited = value; }
        }

        /// <summary>
        /// 静态构造函数，在这里初始化摄像头SDK，仅调用一次
        /// </summary>
        static DaHuaVMSCamera()
        {
            try
            {
                DaHuaVMSCamera.IsSDKInited = (0 == DaHuaVMSDll.KsCamera_DaHuaVMS_Init()) ? false : true;
            }
            catch (Exception e)
            {
                throw new CameraException("无法初始化摄像头SDK", e.Message);
            }
        }

        private DaHuaVMSCamera()
        {

        }

        ~DaHuaVMSCamera()
        {
            if (IsSDKInited)
            {
                try
                {
                    DaHuaVMSDll.KsCamera_DaHuaVMS_Cleanup();
                }
                catch (Exception e)
                {
                    throw new CameraException("无法注销摄像头SDK", e.Message);
                }
            }
        }

        public bool Capture(DaHuaVMS cameraParam, string strImageFile)
        {
            return (0 != DaHuaVMSDll.KsCamera_DaHuaVMS_CaptureImage(cameraParam.RealPlayHandle, strImageFile));
        }

        public bool Close(DaHuaVMS cameraParam)
        {
            if (0 != cameraParam.DevHandle)
            {
                if (0 == DaHuaVMSDll.KsCamera_DaHuaVMS_StopRealPlay(cameraParam.RealPlayHandle))
                {
                    throw new CameraException("无法停止播放");
                }
                else
                {
                    if (0 == DaHuaVMSDll.KsCamera_DaHuaVMS_LogOut(cameraParam.DevHandle))
                    {
                        throw new CameraException("无法注销");
                    }
                    else
                    {
                        cameraParam.DevHandle = 0;
                    }
                }
            }

            return true;
        }

        public bool Open(DaHuaVMS cameraParam)
        {
            if (0 != cameraParam.DevHandle)
            {
                return false;
            }

            try
            {
                cameraParam.DevHandle = DaHuaVMSDll.KsCamera_DaHuaVMS_Login(
                    cameraParam.Ipaddr,
                    (byte)cameraParam.Port,
                    cameraParam.Accesscout,
                    cameraParam.Password,
                    cameraParam.ShowMsg);
            }
            catch (Exception e)
            {
                throw new CameraException("无法登录摄像头", e.Message);
            }

            if (0 == cameraParam.DevHandle)
            {
                throw new CameraException("登录摄像头失败");
            }

            return true;
        }

        public bool Play(DaHuaVMS cameraParam)
        {
            if (0 == cameraParam.DevHandle)
            {
                // 还未打开摄像头
                return false;
            }

            if (0 != cameraParam.RealPlayHandle)
            {
                // 正在播放
                return true;
            }

            try
            {
                cameraParam.RealPlayHandle = DaHuaVMSDll.KsCamera_DaHuaVMS_RealPlay(cameraParam.DevHandle, (uint)cameraParam.Channel, cameraParam.PlayWindow);
            }
            catch (Exception e)
            {
                throw new CameraException("无法播放大华VMS摄像头", e.Message);
            }

            if (0 == cameraParam.RealPlayHandle)
            {
                throw new CameraException("播放大华VMS摄像头失败");
            }

            return true;
        }

        public bool Stop(DaHuaVMS cameraParam)
        {
            if (0 != cameraParam.RealPlayHandle)
            {
                if (0 == DaHuaVMSDll.KsCamera_DaHuaVMS_StopRealPlay(cameraParam.RealPlayHandle))
                {
                    throw new CameraException("无法停止播放");
                }
                else
                {
                    cameraParam.RealPlayHandle = 0;
                }
            }

            return true;
        }
    }
}
